package com.hyy.taobao.dao.impl;

import com.hyy.taobao.api.entity.User;
import com.hyy.taobao.dao.IUserDao;

import java.sql.*;
import java.util.ResourceBundle;

public class UserDaoImpl implements IUserDao {
    @Override
    public boolean insertUser(User user) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        ResourceBundle resourceBundle = ResourceBundle.getBundle("JDBC");
        String driver = resourceBundle.getString("driver");
        String url = resourceBundle.getString("url");
        String user1 = resourceBundle.getString("user");
        String pwd = resourceBundle.getString("password");
        String checkSql = resourceBundle.getString("checkSql");
        String insertSql = resourceBundle.getString("insertSql");
        try {
            Class.forName(driver);
            connection = DriverManager.getConnection(url, user1, pwd);
            preparedStatement = connection.prepareStatement(checkSql);
            preparedStatement.setString(1, user.getUserName());
            resultSet = preparedStatement.executeQuery();
            if (!"".equals(user.getUserName()) || !"".equals(user.getUserPassword()) ||!"".equals(user.getSecurityQuestion())||user.getSecurityAnswer()!=""){
                if (!resultSet.next()) {
                    preparedStatement = connection.prepareStatement(insertSql);
                    preparedStatement.setString(1, user.getUserName());
                    preparedStatement.setString(2, user.getUserPassword());
                    preparedStatement.setString(3, user.getSecurityQuestion());
                    preparedStatement.setString(4, user.getSecurityAnswer());
                    preparedStatement.executeUpdate();
                } else {
                    System.out.println("用户已存在！");
                    return false;
                }
            }else {
                return false;
            }
        } catch (Exception e) {
            System.out.println("注册用户失败！");
            e.printStackTrace();
            return false;
        } finally {
            try {
                if (resultSet != null) {
                    resultSet.close();
                }
            } catch (SQLException e) {
                System.out.println("ResultSet关闭失败！");
                e.printStackTrace();
                return false;
            }
            try {
                if (preparedStatement != null) {
                    preparedStatement.close();
                }
            } catch (SQLException e) {
                System.out.println("PreparedStatement关闭失败！");
                e.printStackTrace();
                return false;
            }
            try {
                if (connection != null) {
                    connection.close();
                }
            } catch (SQLException e) {
                System.out.println("Connection关闭失败！");
                e.printStackTrace();
                return false;
            }
        }
        return true;
    }

    @Override
    public boolean deleteUser(User user) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        ResourceBundle resourceBundle = ResourceBundle.getBundle("JDBC");
        String driver = resourceBundle.getString("driver");
        String url = resourceBundle.getString("url");
        String user1 = resourceBundle.getString("user");
        String pwd = resourceBundle.getString("password");
        String checkSql = resourceBundle.getString("checkSql");
        String deleteSql = resourceBundle.getString("deleteSql");
        String checkGoodSql = resourceBundle.getString("checkGoodSql");
        String deleteGoodsSql = resourceBundle.getString("deleteGoodsSql");
        try {
            Class.forName(driver);
            connection = DriverManager.getConnection(url, user1, pwd);
            preparedStatement = connection.prepareStatement(checkSql);
            preparedStatement.setString(1, user.getUserName());
            resultSet = preparedStatement.executeQuery();
            if (resultSet.next()) {
                preparedStatement = connection.prepareStatement(deleteSql);
                preparedStatement.setString(1, user.getUserName());
                preparedStatement.setString(2, user.getUserPassword());
                preparedStatement.setString(3, user.getSecurityAnswer());
                int count = preparedStatement.executeUpdate();
                if (count == 0) {
                    return false;
                }
                preparedStatement = connection.prepareStatement(checkGoodSql);
                preparedStatement.setString(1, user.getUserName());
                resultSet = preparedStatement.executeQuery();
                if (resultSet.next()){
                    preparedStatement = connection.prepareStatement(deleteGoodsSql);
                    preparedStatement.setString(1, user.getUserName());
                    int count1 = preparedStatement.executeUpdate();
                }else {
                    System.out.println("用户购物车里没有东西！");
                }
            } else {
                System.out.println("用户可能不存在/用户信息错误！");
                return false;
            }
        } catch (Exception e) {
            System.out.println("dao用户删除失败！");
            e.printStackTrace();
            return false;
        } finally {
            try {
                if (resultSet != null) {
                    resultSet.close();
                }
            } catch (SQLException e) {
                System.out.println("ResultSet关闭失败！");
                e.printStackTrace();
                return false;
            }
            try {
                if (preparedStatement != null) {
                    preparedStatement.close();
                }
            } catch (SQLException e) {
                System.out.println("PreparedStatement关闭失败！");
                e.printStackTrace();
                return false;
            }
            try {
                if (connection != null) {
                    connection.close();
                }
            } catch (SQLException e) {
                System.out.println("Connection关闭失败！");
                e.printStackTrace();
                return false;
            }
        }
        return true;
    }

    @Override
    public User userLogin(User user) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        User MyUser;
        ResourceBundle resourceBundle = ResourceBundle.getBundle("JDBC");
        String driver = resourceBundle.getString("driver");
        String url = resourceBundle.getString("url");
        String user1 = resourceBundle.getString("user");
        String pwd = resourceBundle.getString("password");
        String userLoginSql = resourceBundle.getString("userLoginSql");
        try {
            Class.forName(driver);
            connection = DriverManager.getConnection(url, user1, pwd);
            preparedStatement = connection.prepareStatement(userLoginSql);
            preparedStatement.setString(1, user.getUserName());
            preparedStatement.setString(2, user.getUserPassword());
            resultSet = preparedStatement.executeQuery();
            if (resultSet.next()) {
                String userName = resultSet.getString("userName");
                String userPassword = resultSet.getString("userPassword");
                String question = resultSet.getString("userSecurityQuestion");
                String answer = resultSet.getString("userSecurityAnswer");
                MyUser = new User(userName, userPassword, question, answer);
            } else {
                System.out.println("用户可能不存在/用户信息错误！");
                return null;
            }
        } catch (Exception e) {
            System.out.println("dao用户登录失败！");
            e.printStackTrace();
            return null;
        }
        return MyUser;
    }

    @Override
    public boolean updateUser(User user) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        ResourceBundle resourceBundle = ResourceBundle.getBundle("JDBC");
        String driver = resourceBundle.getString("driver");
        String url = resourceBundle.getString("url");
        String user1 = resourceBundle.getString("user");
        String pwd = resourceBundle.getString("password");
        String checkSql = resourceBundle.getString("checkSql");
        String updateSql = resourceBundle.getString("updateSql");
        try {
            Class.forName(driver);
            connection = DriverManager.getConnection(url, user1, pwd);
            preparedStatement = connection.prepareStatement(checkSql);
            preparedStatement.setString(1, user.getUserName());
            resultSet = preparedStatement.executeQuery();
            if (resultSet.next()) {
                preparedStatement = connection.prepareStatement(updateSql);
                preparedStatement.setString(1, user.getUserPassword());
                preparedStatement.setString(2, user.getUserName());
                preparedStatement.executeUpdate();
            } else {
                System.out.println("用户信息修改失败");
                return false;
            }
        } catch (Exception e) {
            System.out.println("用户信息修改失败！");
            e.printStackTrace();
            return false;
        } finally {
            try {
                if (resultSet != null) {
                    resultSet.close();
                }
            } catch (SQLException e) {
                System.out.println("ResultSet关闭失败！");
                e.printStackTrace();
                return false;
            }
            try {
                if (preparedStatement != null) {
                    preparedStatement.close();
                }
            } catch (SQLException e) {
                System.out.println("PreparedStatement关闭失败！");
                e.printStackTrace();
                return false;
            }
            try {
                if (connection != null) {
                    connection.close();
                }
            } catch (SQLException e) {
                System.out.println("Connection关闭失败！");
                e.printStackTrace();
                return false;
            }
        }
        return true;
    }
}
